﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomerService
{
    public partial class NewRMA : System.Web.UI.Page
    {
        private SalesManagerEntities _sme;

        protected void Page_Load(object sender, EventArgs e)
        {
            _sme = (SalesManagerEntities)Session["sme"];
            if (!IsPostBack)
            {
                int ownerID = (int)Session["OwnerID"];
                int orderID = int.Parse(Request.QueryString["OrderID"]);
                int rmaReturnID;

                if (txtRmaReturnID.Text == string.Empty)
                {
                    SalesManagerEntities sme = new SalesManagerEntities();

                    // Get the OrderHeader record
                    tblOrderHeader orderHeader =
                        (tblOrderHeader)_sme.GetObjectByKey(new System.Data.EntityKey(
                            "SalesManagerEntities.tblOrderHeader", "OrderID", orderID));

                    try
                    {
                        rmaReturnID = (int)_sme.spNewRMA(orderID).FirstOrDefault();
                    }
                    catch (Exception)
                    {
                        if (!Page.ClientScript.IsStartupScriptRegistered("PostBack"))
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "PostBack",
                                "<script  language='javascript' type='text/javascript'>" +
                                "alert( 'All product from this order is already returned.' ); " +
                                "window.close('fred');" +
                                "</script>"
                                );
                        return;
                    }

                    txtRmaReturnID.Text = rmaReturnID.ToString();
                }
                rmaReturnID = int.Parse(txtRmaReturnID.Text);
                tblRMAReturn rmaReturn =
                    (tblRMAReturn)_sme.GetObjectByKey(new System.Data.EntityKey(
                        "SalesManagerEntities.tblRMAReturn", "RMAReturnID", rmaReturnID));

                ListView1.DataSource = rmaReturn.ReturnDetails;
                ListView1.DataBind();
            }
            else
            {
                int rmaReturnID = int.Parse(txtRmaReturnID.Text);
                tblRMAReturn rmaReturn =
                    (tblRMAReturn)_sme.GetObjectByKey(new System.Data.EntityKey(
                        "SalesManagerEntities.tblRMAReturn", "RMAReturnID", rmaReturnID));

                // Put the qty values back into the ReturnDetail table
                for (int i = 0; i < ListView1.Items.Count; i++)
                {
                    int key = (int)ListView1.DataKeys[i].Value;
                    int qty;
                    if (int.TryParse(((TextBox)ListView1.Items[i].FindControl("txtQty")).Text, out qty))
                    {
                        rmaReturn.OrderHeader.Customer.cachedReturnDetails.First(cp => cp.ReturnDetailID == key).Qty = qty;
                    }
                }
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            _sme.SaveChanges();
            // Trigger the RMAReturn panel to refresh.            
            if (!Page.ClientScript.IsStartupScriptRegistered("PostBack"))
                Page.ClientScript.RegisterStartupScript(this.GetType(), "PostBack",
                    "<script  language='javascript' type='text/javascript'>" +
                    "opener.__doPostBack('" + Request.QueryString["PanelID"] + "', 'UpdateDetails'); window.close('fred');" +
                    "</script>"
                    );
        }
    }
}